package game.module.turntables.dao;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.Files;

import game.module.template.ItemTemplate;
import game.module.template.TurntableTemplate;
import lion.common.Reloadable;
import lion.math.RandomDispatcher;

public class TurntablesTemplateCache implements Reloadable {

	private static Logger logger = LoggerFactory.getLogger(TurntablesTemplateCache.class);
	
	static class SingletonHolder{
		static TurntablesTemplateCache instance = new TurntablesTemplateCache();
	}
	public static TurntablesTemplateCache getInstance() {
		return SingletonHolder.instance;
		
	}
	private transient List<TurntableTemplate> templateList;
	
	private transient List<TurntableTemplate> login_turntable;
	
	private transient List<TurntableTemplate> score_turntable;
	@Override
	public void reload() {
		// TODO Auto-generated method stub
		try {
			String jsonStr = Files.asCharSource(new File("data/turntable.json"), Charset.forName("UTF-8")).read();
			ObjectMapper mapper = new ObjectMapper();
			templateList = mapper.readValue(jsonStr, new TypeReference<List<TurntableTemplate>>( ) {
			});
			//logger.info("turntables size={}",templateList.size());
			List<TurntableTemplate> login = new ArrayList<>();
			List<TurntableTemplate> score = new  ArrayList<>();
			for(TurntableTemplate atemp:templateList) {
				if(atemp.getType()==1)
					login.add(atemp);
				if(atemp.getType()==2)
					score.add(atemp);
			}
			login_turntable = login;
			score_turntable = score;
		}catch(Exception e) {
			logger.error("turntables", e);
		}
		logger.error("login turntables ={}",login_turntable.size() );
		logger.error("score turntables ={}",score_turntable.size() );
	}
	public List<TurntableTemplate> getlogin_turntable(){
		return login_turntable;
	}
	public List<TurntableTemplate> getscore_turntable(){
		return score_turntable;
	}
	public List<TurntableTemplate> getturntable(){
		return templateList;
	}
    public static void main(String[] args) {
    //	new TurntablesTemplateCache().reload();
    	//List<TurntableTemplate> t = TurntablesTemplateCache.getInstance().getlogin_turntable();
		RandomDispatcher<Integer> rd = new RandomDispatcher<>();
		rd.put(10, 1000);
		rd.put(20, 2000);
		for (int i = 0; i < 10; i++) {
			int a = rd.random();
			System.out.println("===" + a);
		}
	}
}
